import re
import os
import json
import zhipuai
def remove_markdown_code_block(text):
    # 创建正则表达式来匹配代码块标记
    re_pattern = r"```.*?\n"
    text = re.sub(re_pattern, "", text)

    # 去除剩余的代码块结束标记
    re_pattern = r"```"
    text = re.sub(re_pattern, "", text)

    return text

def traverse_dir(dir_path, header_files, source_files):
    for entry in os.scandir(dir_path):
        if entry.is_file():
            # 获取文件扩展名
            extension = os.path.splitext(entry.name)[1]
            # 读取头文件内容
            if extension == ".h":
                with open(entry.path, 'r') as file:
                    content = file.read()
                    filename = os.path.splitext(entry.name)[0]
                    header_files[filename] = content
            # 读取C源文件内容
            elif extension == ".c":
                with open(entry.path, 'r') as file:
                    content = file.read()
                    filename = os.path.splitext(entry.name)[0]
                    source_files[filename] = content
        elif entry.is_dir():
            # 递归遍历子目录
            traverse_dir(entry.path, header_files, source_files)


def translate_prompt():
    ret = []
    directory = "../Input/01-Primary/src"
    header_files = {}
    source_files = {}

    traverse_dir(directory, header_files, source_files)

    for filename, header_content in header_files.items():
        if filename in source_files:
            prompt = f"Translate the following C code to Rust: {source_files[filename]}, "
            ret.append(prompt)

    return ret

import os
import shutil

def copy_files_with_extensions(source_dir, target_dir):
    """
    Copy files with specific extensions from source directory to target directory.

    Args:
    source_dir (str): The source directory to search for files.
    target_dir (str): The target directory to copy files to.
    extensions (list): A list of file extensions to copy.
    """
    extensions = ['.c', '.h', '.cpp']

      # 如果目标目录不存在，则创建它
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    for root, dirs, files in os.walk(source_dir):
        for file in files:
            if any(file.endswith(ext) for ext in extensions):
                file_path = os.path.join(root, file)
                shutil.copy(file_path, target_dir)

# 设置源目录和目标目录
source_directory = '/path/to/source/directory'
target_directory = '/path/to/target/directory'

# 设置要复制的文件扩展名

# 调用函数
copy_files_with_extensions(source_directory, target_directory)

# 示例用法
header_files = {}
source_files = {}
traverse_dir("/path/to/directory", header_files, source_files)